/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Boundaries.InternalFrame_Rekap;
import Entities.Sewa;
import TableModel.Persewaan_List;
import TableModel.Persewaan_Rekap_TableModel;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Tea-Nose
 */
public class Rekap_Controller {

    public Rekap_Controller() {
    }


    public Rekap_Controller(InternalFrame_Rekap frame){
        InternalFrame_Rekap in_Rekap= frame;
        try {
            String reportSource = "./reports/Rekap_Report.jasper";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("bulanSewa", in_Rekap.comboBox_Bulan.getSelectedItem().toString());
            params.put("tahunSewa", String.valueOf(in_Rekap.yearChooser_Tahun.getYear()));
            params.put("jumKendaraan", in_Rekap.jLabel2.getText());
            params.put("totalPendapatan", in_Rekap.jLabel4.getText());

            JasperPrint jasperPrint = JasperFillManager.fillReport("./reports/Rekap_Report.jasper", params, new JRTableModelDataSource(in_Rekap.table_Rekap.getModel()));

            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public void getDataSewa(InternalFrame_Rekap frame){
        InternalFrame_Rekap in_Rekap= frame;
        Persewaan_Rekap_TableModel persewaan_Rekap_TableModel;
        try {
            // TODO add your handling code here:
            int jumlah = 0;
            in_Rekap.table_Rekap.setVisible(true);
            String bulan=String.valueOf(in_Rekap.comboBox_Bulan.getSelectedIndex()+1);


            String tahun = "-" + String.valueOf(in_Rekap.yearChooser_Tahun.getYear());
            String parameter = bulan + tahun;

            List<Sewa> sewas = Persewaan_List.getKendaraanList().getPersewaanRekap(parameter);
            persewaan_Rekap_TableModel = new Persewaan_Rekap_TableModel(sewas);
            in_Rekap.table_Rekap.setModel(persewaan_Rekap_TableModel);

            in_Rekap.jLabel2.setText("" + in_Rekap.table_Rekap.getRowCount());
            for (int i = 0; i < in_Rekap.table_Rekap.getRowCount(); i++) {
                jumlah = jumlah + (Integer.parseInt(in_Rekap.table_Rekap.getValueAt(i, 5).toString()) + Integer.parseInt(in_Rekap.table_Rekap.getValueAt(i, 7).toString()));
            }
            in_Rekap.jLabel4.setText("" + jumlah);

            if (jumlah==0){
                in_Rekap.button_cetakRekap.setEnabled(false);
                JOptionPane.showMessageDialog(null, "Maaf pada bulan tersebut tidak terjadi transaksi penyewaan");
            }else{
                in_Rekap.button_cetakRekap.setEnabled(true);
            }
        } catch (SQLException ex) {
            Logger.getLogger(InternalFrame_Rekap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
